<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Admin\Manager;
use Auth;
use App\Admin\Role;

class ManagerController extends Controller
{
    public function lst(Request $request)
    {

        if ($request->isMethod('post')) {

            $pageNum = $request->input('curr');
            $limit = $request->input('nums');
            $count = Manager::count();
            $mess = "";
            $sta = 0;
            // $data = Manager::paginate($nums)->toArray();
            //$data=Manager::get()->toArray();
            //dd($limit);
            $page = $pageNum - 1;
            if ($page != 0) {
                $page = $limit * $page;
                //$limit=$limit*$pageNum;
            }
            //$data = Manager::offset($page)->limit($limit)->get()->toArray();

            $data = Manager::offset($page)->limit($limit)->get();
            foreach ($data as $val) {
                if ($val->Role) {
                    $val->role_id = $val->Role->role_name;
                }
            }
            $data = $data->toArray();

            //这里把数据中性别的处理放到了前端js代码中
            // foreach($data as $val)
            // {
            //     if($val->gender=='1')$val->gender='男';
            //     elseif($val->gender=='2')$val->gender='女';
            //     else $val->gender='保密';
            // }
            return (compact('data', 'count', 'mess', 'sta'));
        } else return view('admin.manager.lst');
    }
    //这是管理员的添加操作
    public function add(Request $request)
    {
        //如果是get请求，那么就显示视图
        if ($request->isMethod('get')) {
            $Role = Role::get();
            return view('admin.manager.add', compact('Role'));
            //如果是post请求，那么就处理表单传来的数据
        } else {
            // dd($request->Input('add_data'));
            // $manager = Manager::create($request->Input('add_data'));
            // $manager->password = bcrypt($request->input('password'));
            $res = $request->Input('add_data');
            $manager = new Manager();
            $manager->insert([
                'status'      => $res['status'],
                'username'      => $res['username'],
                'gender'      => $res['gender'],
                'mobile'      => $res['mobile'],
                'email'      => $res['email'],
                'role_id'      => $res['role_id'],
                'password'      => bcrypt($res['password'])
            ]);
            // dd($manager);
            //保存到数据库

            if ($manager) $status = 'ok';
            else $status = 'error';
            return (compact('status'));
        }
    }

    public function del(Request $request)
    {
        //d($request->input('del_id'));
        // $status='d';
        // dd($request->input('del_num'));

        /* Model的delete()方法返回bool类型，destory()返回影响的行数 */
        if ($request->input('del_num') == 'one') {
            $id = $request->input('del_id');
            $result = Manager::where('id', $id)->delete();

            // dd($result);
            if ($result) $status = 'ok';
            else $status = 'error';
            return (compact('status'));
        } elseif ($request->input('del_num') == 'several') {
            //  dd($request->input('del_id'));
            $ids = array_values($request->input('del_id'));
            $nums = Manager::destroy($ids);
            if ($nums) $status = 'ok';
            else $status = 'error';
            return (compact('status', 'nums'));
        }
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('get')) {

            //$id=$request->Input('id');
            $data = Manager::where('id', $id)->get();
            //dd($data);
            return view('admin.manager.edit', compact('data', 'id'));
        } else {
            //处理表单的数据
            // $manager=Manager::create($request->Input('add_data'));
            // $manager->password=bcrypt($request->input('password'));
            $res = $request->Input('edit_data');

            $edit_manager = Manager::where('id', $id);
            // dd($edit_manager);
            $rows = $edit_manager->update([
                'status'      => $res['status'],
                'username'      => $res['username'],
                'gender'      => $res['gender'],
                'mobile'      => $res['mobile'],
                'email'      => $res['email'],
                'role_id'      => $res['role_id'],
                'password'      => bcrypt($res['password'])

            ]);
            //dd($rows);
            if ($rows) $status = 'ok';
            else $status = 'error';
            return (compact('status'));


            //保存到数据库

        }
    }
}
